package com.ruoyi.algorithm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.algorithm.entity.AlgorithmType;
import com.ruoyi.algorithm.entity.AlgorithmTypeGroup;
import com.ruoyi.algorithm.repository.AlgorithmTypeGroupRepository;
import com.ruoyi.algorithm.repository.AlgorithmTypeRepository;
import com.ruoyi.algorithm.service.AlgorithmTypeService;
import com.ruoyi.algorithm.util.MybatisUtils;
import com.beihu.sightplus.common.authorization.service.UserAuthorization;
import com.beihu.sightplus.common.request.PageRequest;
import com.beihu.sightplus.common.response.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.logging.Logger;

/**
 * @author dyk
 * @date 2021/7/16 11:48
 * @description
 */
@Service
public class AlgorithmTypeServiceImpl implements AlgorithmTypeService {

    private Logger logger = Logger.getLogger(AlgorithmType.class.getName());

    @Resource
    private AlgorithmTypeRepository algorithmTypeRepository;
    @Resource
    private AlgorithmTypeGroupRepository algorithmTypeGroupRepository;

    /**
     * 添加算法类别组
     *
     * @param algorithmTypeGroup
     * @return
     */
    @Override
    public boolean addAlgorithmTypeGroup(AlgorithmTypeGroup algorithmTypeGroup) {
        algorithmTypeGroup.setDelFlag("0");
        algorithmTypeGroup.setCreateTime(new Date());
        algorithmTypeGroup.setCreateUser(UserAuthorization.getUserAuthorization().getId());
        return algorithmTypeGroupRepository.save(algorithmTypeGroup);
    }

    /**
     * 添加算法分类
     *
     * @param algorithmType
     * @return
     */
    @Override
    public boolean addAlgorithmType(AlgorithmType algorithmType) {
        algorithmType.setDelFlag("0");
        algorithmType.setCreateTime(new Date());
        algorithmType.setCreateUser(UserAuthorization.getUserAuthorization().getId());
        return algorithmTypeRepository.save(algorithmType);
    }

    /**
     * 删除算法类别组
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteAlgorithmTypeGroup(Long id) {

        int count = algorithmTypeRepository.count(Wrappers.lambdaQuery(AlgorithmType.class)
                .eq(AlgorithmType::getDelFlag, "0")
                .eq(AlgorithmType::getAlgorithmTypeGroupId, id));
        if (count > 0) {
            logger.info("存在子节点，无法删除");
            return false;
        }

        AlgorithmTypeGroup algorithmTypeGroup = new AlgorithmTypeGroup();
        algorithmTypeGroup.setId(id);
        algorithmTypeGroup.setDelFlag("1");
        algorithmTypeGroup.setUpdateTime(new Date());
        algorithmTypeGroup.setUpdateUser(UserAuthorization.getUserAuthorization().getId());
        return algorithmTypeGroupRepository.updateById(algorithmTypeGroup);
    }

    /**
     * 删除算法分类
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteAlgorithmType(Long id) {
        AlgorithmType algorithmType = new AlgorithmType();
        algorithmType.setId(id);
        algorithmType.setDelFlag("1");
        algorithmType.setUpdateTime(new Date());
        algorithmType.setUpdateUser(UserAuthorization.getUserAuthorization().getId());
        return algorithmTypeRepository.updateById(algorithmType);
    }

    /**
     * 修改算法类别组
     *
     * @param algorithmTypeGroup
     * @return
     */
    @Override
    public boolean updateAlgorithmTypeGroup(AlgorithmTypeGroup algorithmTypeGroup) {
        algorithmTypeGroup.setUpdateUser(UserAuthorization.getUserAuthorization().getId());
        algorithmTypeGroup.setUpdateTime(new Date());
        return algorithmTypeGroupRepository.updateById(algorithmTypeGroup);
    }

    /**
     * 修改算法分类
     *
     * @param algorithmType
     * @return
     */
    @Override
    public boolean updateAlgorithmType(AlgorithmType algorithmType) {
        algorithmType.setUpdateUser(UserAuthorization.getUserAuthorization().getId());
        algorithmType.setUpdateTime(new Date());
        return algorithmTypeRepository.updateById(algorithmType);

    }

    /**
     * 查询算法类别组详情
     *
     * @param id
     * @return
     */
    @Override
    public AlgorithmTypeGroup detailAlgorithmTypeGroup(Long id) {
        AlgorithmTypeGroup detail = algorithmTypeGroupRepository.getOne(Wrappers.lambdaQuery(AlgorithmTypeGroup.class)
                .eq(AlgorithmTypeGroup::getId, id));
        return detail;
    }

    /**
     * 查询算法分类详情
     *
     * @param id
     * @return
     */
    @Override
    public AlgorithmType detailAlgorithmType(Long id) {
        AlgorithmType detail = algorithmTypeRepository.getOne(Wrappers.lambdaQuery(AlgorithmType.class)
                .eq(AlgorithmType::getId, id));
        return detail;
    }

    /**
     * 分页查询算法分类组
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult listAlgorithmTypeGroup(PageRequest pageRequest) {
        String jsonString = JSON.toJSONString(pageRequest);
        AlgorithmTypeGroup algorithmTypeGroup = JSONObject.parseObject(jsonString, AlgorithmTypeGroup.class);
        QueryWrapper<AlgorithmTypeGroup> queryWrapper = Wrappers.query(algorithmTypeGroup);
        queryWrapper.lambda().eq(AlgorithmTypeGroup::getDelFlag, "0");

        // 构建排序条件
        MybatisUtils.getOrderBy(queryWrapper, pageRequest);

        // 构建分页
        Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());

        // 查询分页
        Page result = algorithmTypeGroupRepository.page(page, queryWrapper);

        // 构建结果
        PageResult pageResult = new PageResult();
        pageResult.setContent(result.getRecords());
        pageResult.setTotalSize(result.getTotal());
        pageResult.setTotalPages(result.getPages());
        return pageResult;
    }

    /**
     * 分页查询算法分类组
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult listAlgorithmType(PageRequest pageRequest) {
        String jsonString = JSON.toJSONString(pageRequest);
        AlgorithmType algorithmType = JSONObject.parseObject(jsonString, AlgorithmType.class);
        QueryWrapper<AlgorithmType> queryWrapper = Wrappers.query(algorithmType);
        queryWrapper.lambda().eq(AlgorithmType::getDelFlag, "0");

        // 构建排序条件
        MybatisUtils.getOrderBy(queryWrapper, pageRequest);

        // 构建分页
        Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());

        // 查询分页
        Page result = algorithmTypeRepository.page(page, queryWrapper);

        // 构建结果
        PageResult pageResult = new PageResult();
        pageResult.setContent(result.getRecords());
        pageResult.setTotalSize(result.getTotal());
        pageResult.setTotalPages(result.getPages());
        return pageResult;
    }
}
